package edu.infomatik.tundra.components;

import java.util.Locale;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.PersistentLocale;

import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.USTaskBean;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.logic.security.UserTypes;
import edu.infomatik.tundra.pages.*;

@SuppressWarnings("unused")
public class MainFrame {
	@Inject
	private PersistentLocale persistentLocale;
	@Inject
	private Messages message;
	@Inject
	@Path("images/flag.gif")
	private Asset currentZastava;
	@Inject
	private ComponentResources resources;
	
	@InjectPage
	private Index i;

	@SessionState
    private SystemAccessBean user;
    private boolean userExists;
	@SessionState
	private WorkBean project;
	@SessionState
	private UStoryBean story;
	@SessionState
	private USTaskBean task;
	@SessionState
	private SprintBean sprint;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	@Parameter(required = true, defaultPrefix = "literal")
	private String pageTitle;

    @OnEvent (component="logOutClick")
    public Object onLogOut(){
    	user = null;
    	return Index.class;
    }
    Object onActionFromCreateTask(){
    	newWork=new Boolean(true);
    	task=null;
    	return "CreateTask";
    }
    String onActionFromCreateSprint(){
    	newWork = new Boolean(true);
    	sprint=null;
    	return "CreateSprint";
    }
    
    String onActionFromCreateProject(){
    	newWork = new Boolean(true);
    	project=null;
    	return "CreateProject";
    }
    
    String onActionFromCreateUStory(){
    	newWork = new Boolean(true);
    	story=null;
    	return "CreateUStory";
    }
        
    public String getUserTypeLabelText(){
		String result="";
		if (null != user && user.getUserName()!=null)
			result="<strong>"+message.get("UserTypes."+UserTypes.get(user.getUserType()).getName())+"</strong>"; 
		return result;    	
    }
    
	public String getPageTitle(){
		return pageTitle;
	}
		
	public String getCurrentLang() {
		if (persistentLocale.get()==null)
			persistentLocale.set(Locale.ENGLISH);
		return persistentLocale.get().getDisplayLanguage(persistentLocale.get());
	}
	
	public Asset getCurrentZastava() {
		return currentZastava;
	}
	
	private String getPageName(){
		Component page = resources.getContainer();
		return page.getClass().getName();
	}

	public SystemAccessBean getUser() {
		return user;
	}

	public boolean isUserExists() {
		return userExists;
	}
	
	Object onActionFromLogOffLink(){
		user=null;
		return i.getClass();
	}
	
	public boolean isIndex()	{
		return getPageName().equals(Index.class.getName());
	}
	public boolean isRegistration()	{
		return getPageName().equals(Registration.class.getName());
	}
	public boolean isHelp()	{
		return getPageName().equals(Help.class.getName());
	}
	public boolean isProjectList()	{
		return getPageName().equals(ProjectList.class.getName());
	}
	public boolean isWorkList()	{
		return getPageName().equals(WorkList.class.getName());
	}
	public boolean isUnfinishedList()	{
		return getPageName().equals(UnfinishedList.class.getName());
	}
	public boolean isCreateProject()	{
		return getPageName().equals(CreateProject.class.getName());
	}
	public boolean isCreateUStory()	{
		return getPageName().equals(CreateUStory.class.getName());
	}
	public boolean isCreateSprint()	{
		return getPageName().equals(CreateSprint.class.getName());
	}
	public boolean isCreateTask()	{ 
		return getPageName().equals(CreateTask.class.getName());
	}
	
//	public boolean isTheProject()	{
//		return getPageName().equals(TheProject.class.getName());
//	}
//	public boolean isTheStory()	{
//		return getPageName().equals(TheStory.class.getName());
//	}
}
